package com.kqzc.transportcar.base

import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.databinding.ViewDataBinding
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.app.mvvmkotlindemo.R
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.everyline.baselibrary.base.fragment.BaseVMFragment
import com.everyline.baselibrary.base.viewmodel.BaseViewModel
import com.everyline.baselibrary.ext.dismissLoadingExt
import com.everyline.baselibrary.ext.showLoadingExt
import com.everyline.baselibrary.network.ApiException
import com.kqzc.transportcar.interfaceListener.DialogButtonClickListener

/**
 * Created by on 2021/4/8.
 * 天天挂线
 *  746608431@qq.com
 *  描述:TODO
 */

abstract class BaseFragment<VM : BaseViewModel, DB : ViewDataBinding> : BaseVMFragment<VM, DB>() {

    private var dialogTwoButton: MaterialDialog? = null


    protected fun showDialogTwoButton(
        title: String,
        message: String,
        buttonNames: Array<String>,
        dialogButtonClickListener: DialogButtonClickListener
    ) {

        dialogTwoButton = this.let {
            MaterialDialog(mContext)
                .cancelable(false)
                .cancelOnTouchOutside(false)
                .customView(
                    R.layout.dialog_two_button_layout, null,
                    scrollable = true,
                    noVerticalPadding = true,
                    horizontalPadding = false,
                    dialogWrapContent = true
                )

        }
        dialogTwoButton?.getCustomView()?.run {
            this.findViewById<TextView>(R.id.tv_dialog_title).text = title
            this.findViewById<TextView>(R.id.tv_dialog_message).text = message

            if (buttonNames != null && buttonNames.isNotEmpty()) {
                when (buttonNames.size) {

                    2 -> {

                        this.findViewById<TextView>(R.id.tv_dialog_cancle).apply {
                            text = buttonNames[0]
                            setOnClickListener {
                                dialogTwoButton?.dismiss()
                                dialogButtonClickListener.negativeButtonClick()
                            }
                        }
                        this.findViewById<TextView>(R.id.tv_dialog_ok).apply {
                            text = buttonNames[1]
                            setOnClickListener {
                                dialogTwoButton?.dismiss()
                                dialogButtonClickListener.positiveButtonClick()
                            }
                        }
                    }


                    1 -> {
                        this.findViewById<TextView>(R.id.tv_dialog_cancle).apply {
                            visibility = View.GONE
                        }
                        this.findViewById<View>(R.id.line_mid).apply {
                            visibility = View.GONE
                        }

                        this.findViewById<TextView>(R.id.tv_dialog_ok).apply {
                            text = buttonNames[0]
                            setOnClickListener {
                                dialogTwoButton?.dismiss()
                                dialogButtonClickListener.positiveButtonClick()
                            }
                        }
                    }
                }

            }

        }
        dialogTwoButton?.show()
    }
    override fun showLoading(message: String) {
        showLoadingExt()
    }

    override fun dismissLoading() {
        dismissLoadingExt()

    }

    protected fun handleThrowable(apiException: ApiException) {
        var code = apiException.errCode
        var msg = apiException.errorMsg
        LogUtils.d("handleThrowable==code==${code}")
        msg?.let {
            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(it)
        }
    }

    protected fun toastMsg(message: String) {
        message?.let {
            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(it)
        }
    }

}